/**
 * myHeader vuex
 */
// import posts from '@/plugins/request'
import storage from '@/plugins/localStorage'

// initial state
const state = {
  tipsList: [],
  emailList: [],
}
// getters
const getters = {
  getTipsList: state => state.tipsList,
  getEmailList: state => state.emailList,
}
// actions
const actions = {
}

// mutations
const mutations = {
  pushHeaderTips(state, data) {
    let tipsBox = state.tipsList
    let duplicate = tipsBox.find(v => v.taskId === data.taskId)
    // debugger
    if(!duplicate) {
      tipsBox.push(data)
      storage.set('tipsList', JSON.stringify(tipsBox))
    }
  },
  resetHeaderTips(state) {
    state.tipsList = []
  },
  delOnceHeaderTips(state, index) {
    state.emailList.splice(index, 1)
    storage.set('emailList', JSON.stringify(state.emailList))
  },
  pushHeaderEmail(state, data) {
    let emailBox = state.emailList
    let duplicate = emailBox.find(v => v.id === data.id)
    if(!duplicate) {
      emailBox.push(data)
      storage.set('tipsList', JSON.stringify(emailBox))
    }
    state.emailList = emailBox.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime())
  },
  resetHeaderEmail(state) {
    state.emailList = []
  },
  delOnceHeaderEmail(state, index) {
    state.emailList.splice(index, 1)
    storage.set('emailList', JSON.stringify(state.emailList))
  },
}
export default {
  state,
  getters,
  actions,
  mutations
}
